//********************************************************************
//	created:	16:10:2011   0:45
//	filename: 	sslsocket.h
//	author:		tiamo
//	purpose:	ssl socket
//********************************************************************

#pragma once

namespace network
{
	//
	// ssl socket
	//
	class SSLSocket : public AsyncSocket
	{
	public:
		//
		// constructor
		//
		SSLSocket();

		//
		// destructor
		//
		virtual ~SSLSocket();

		//
		// set ssl info
		//
		void setSSLInfo(UniString const& CARoot, UniString const& certFileName, UniString const& keyFileName);

	private:
		//
		// on connect
		//
		virtual void onConnected(bool errorState);

		//
		// real read
		//
		virtual int realRead(void* buffer, uint32_t readLength);

		//
		// real write
		//
		virtual int realWrite(void const* buffer, uint32_t writeLength);

		//
		// check would block error
		//
		virtual bool isWouldBlockError() const;

		//
		// check and set ssl would block error flags
		//
		int checkAndSetSSLWouldBlockErrorFlags(int retValue);

	private:
		//
		// ssl context
		//
		SSL_CTX*															mSSLContext;

		//
		// ssl socket
		//
		SSL*																mSSLSocket;

		//
		// ssl method
		//
		SSL_METHOD const*													mSSLMethod;

		//
		// would block error
		//
		bool																mWouldBlock;

		//
		// ca root
		//
		UniString															mCARoot;

		//
		// cert file name
		//
		UniString															mCertFileName;

		//
		// key file name
		//
		UniString															mKeyFileName;
	};
}
